package com.chase.framerwork.action;

import java.util.List;

import javax.annotation.Resource;

import net.sf.json.JSONObject;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;

import com.chase.framerwork.action.excel.ParseUser;
import com.chase.framerwork.entity.User;
import com.chase.framerwork.excel.Excel;
import com.chase.framerwork.excel.ExcelFactroy;
import com.chase.framerwork.excel.ExcelUtils;
import com.chase.framerwork.message.MessageUtil;
import com.chase.framerwork.service.UserService;

/**
 * 用户操作Action
 * 
 * @author Chase
 * 
 */
@ParentPackage("chase-struts")
@Namespace("/")
@Action(value = "userAction", results = { @Result(name = "input", location = "/framework/user/userInput.jsp"),
        @Result(name = "view", location = "/framework/user/userView.jsp"),
        @Result(name = "list", location = "/framework/user/userList.jsp") })
@Scope("prototype")
public class UserAction extends BaseAction
{

	private static final long serialVersionUID = 7228043685105009379L;

	private UserService userService;
	private User user;

	public UserAction()
	{
		user = new User();
	}

	public String add() throws Exception
	{
		if (user.getPassword().equals(user.getConfirmPassword()))
		{
			userService.save(user);

			addActionMessage("Create success");
			return list();
		}
		else
		{
			addActionMessage("Entered passwords differ");
			return INPUT;
		}
	}

	public String remove() throws Exception
	{

		userService.delete(id);

		addActionMessage("Delete success !");
		return list();
	}

	public String update() throws Exception
	{

		userService.update(user);

		addActionMessage("Update success !");
		
		return list();
	}

	public String edit() throws Exception
	{
		user = userService.load(id);

		return INPUT;
	}

	public String list() throws Exception
	{
		pager = userService.findPager(pager);

		return LIST;
	}

	public String view() throws Exception
	{
		user = userService.load(id);
		
		return VIEW;
	}

	public String removeAll() throws Exception
	{

		userService.delete(ids);

		addActionMessage("Delete success !");
		return list();
	}

	public String search() throws Exception
	{

		List<User> list = userService.search(user);

		JSONObject json = new JSONObject();
		json.put("userList", list);
		outJSON(json);

		return NONE;
	}

	public String exportExcel() throws Exception
	{

		Excel excel = ExcelFactroy.getInstance().createExcel("UserSheet", "UserReport");

		List<User> list = userService.getAllList();

		ExcelUtils.writeExcel(getResponse(), ParseUser.class, excel, list, new String[] { "UserName", "Status",
		        "CreateDate" });

		return NONE;
	}

	@SuppressWarnings("unchecked")
	public String importExcel() throws Exception
	{

		Excel excel = ExcelFactroy.getInstance().readExcel(upload, uploadFileName);

		List<User> userList = (List<User>) ExcelUtils.readExcel(getRequest(), ParseUser.class, excel);

		userService.save(userList);

		return list();
	}

	@Resource
	public void setUserService(UserService userService)
	{
		this.userService = userService;
	}

	public UserService getUserService()
	{
		return userService;
	}

	public User getUser()
	{
		return user;
	}

	public void setUser(User user)
	{
		this.user = user;
	}
}
